import { Engine } from "../../Engine";
import { any } from "../../core/logic";
import { Component, ComponentType } from "../Component";
class Plot extends Component{
    static that=null;
    constructor(opts={}){
        opts.type=ComponentType.Plot;
        super(opts);
        this.mode=any(opts.mode,"mark");//两种模式：mark(标)|draw(绘);
        this._cursor=any(opts.cursor,"");
        this.drawConfig={};
        Plot.that=this;
    }
    set cursor(v){
        this._cursor=v;
        return this;
    }
    get cursor(){
        return this._cursor;
    }
    get pick(){
        return Engine.instance.register.manager(ComponentType.Pick).get("x");
    }
    setPlotContent(opts={}){
        this.drawConfig.entity=opts.entity;
        this.drawConfig.group=opts.group;
        this.drawConfig.instance=any(opts.instance,true);
        this.drawConfig.scale=any(opts.scale,[1,1,1]);
        this.drawConfig.rotation=any(opts.rotation,[0,0,0]);
        this.drawConfig.randomScale=any(opts.randomScale,0);
        this.drawConfig.randomRotation=any(opts.randomRotation,0);
    }
    active(){
        this.off("dblclick",this.deactive);
        this.on("dblclick",this.deactive);
        if(this.mode==="mark"){
            if (this.cursor) {
                Engine.instance.canvas.style.cursor = `url(${this.cursor}),auto`;
            }
            this.pick.select(this.mark);
        }else{
            //暂时不做
        }
        if(Plot.that.callback){
            Plot.that.callback("active");
        }
        
    }
    deactive(){
        this.off("dblclick",this.deactive);
        if(this.mode==="mark"){
            Engine.instance.canvas.style.cursor="default";
            this.pick.offSelect(this.mark);
        }else{
            //暂时不做
        }
        if(Plot.that.callback){
            Plot.that.callback("deactive");
        }
    }
    draw(){
        
    }
    mark(result){
        const {entity,group,scale,randomScale,rotation,randomRotation,instance}=Plot.that.drawConfig;
        if(result.event.originEvent.shiftKey){
            let et=result.entity;
            let instanceIndex=result.instanceIndex;
            if(instanceIndex!=undefined){
                let root=et.getRoot();
                let c=root.getAllInstance();
                for(let i=0;i<c.length;i++){
                    c[i].trans.removeByIndex(instanceIndex); 
                }
                Plot.that.markCallback({type: 'delete', instanceIndex, entity: root});
                //et.trans.removeByIndex(instanceIndex); 
            }
            return;
        }
        if(!entity){
            return;
        }
        const position=result.worldposition;
        let [sx,sy,sz]=scale;
        sx+=Math.random()*randomScale;
        sy+=Math.random()*randomScale;
        sz+=Math.random()*randomScale;
        let [rx,ry,rz]=rotation;
        rx+=Math.random()*randomRotation;
        ry+=Math.random()*randomRotation;
        rz+=Math.random()*randomRotation;
        if(instance){
            if(entity.group=="default"){//未添加到场景
                group.addEntity(entity);
                entity.addInstance({position,scale:[sx,sy,sz],rotation:[rx,ry,rz]},true);
            }else{
                entity.addInstance({position,scale:[sx,sy,sz],rotation:[rx,ry,rz]});
            } 
            if(Plot.that.markCallback){
                Plot.that.markCallback({type: 'add', position,scale:[sx,sy,sz],rotation:[rx,ry,rz]});
            }
        }else{//非实例化绘制
            entity.trans.position=position;
            entity.trans.scale=[sx,sy,sz];
            entity.trans.rotation=[rx,ry,rz];
            if(entity.group=="default"){
                group.addEntity(entity);
                if(Plot.that.markCallback){
                    Plot.that.markCallback({position,scale:[sx,sy,sz],rotation:[rx,ry,rz]});
                }
            }
        }
        
    }
}
export {Plot}